<template>
  <view class="main">
    <!-- 搜索框 -->
    <view class="main_header_seach">
      <search></search>
    </view>
    <!-- 轮播图 -->
    <view class="main_header_swiper">
      <swiper class="swiper1" :indicator-dots="true" circular :autoplay="true" :interval="3000" :duration="1000">
        <swiper-item v-for="(item,index) in swiperList" :key="index">
          <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?cid=' + item.goods_id">
            <image :src="item.image_src" mode="widthFix"></image>
          </navigator>
        </swiper-item>
      </swiper>
    </view>

    <!-- 分类导航 -->
    <view class="main_nav_list">
      <view class="nav_item" v-for="(item,index) in navList" :key="index" @click="toCate(item)">
        <image class="nav_image" :src="item.image_src" mode="widthFix"></image>
      </view>
    </view>

    <!-- 楼梯层部分 -->
    <view class="floor_list">
      <!-- 楼层顶部 -->
      <view class="floor_item" v-for="(item,index) in floorList" :key="index">
        <!-- 最顶上的图片 -->
        <image class="floor_item_image" :src="item.floor_title.image_src"></image>
        <view class="floor">
          <!-- 左面的长图 -->
          <navigator class="floor_left_img" :url="item.product_list[0].url">
            <image class="left_HegihtImage" :src="item.product_list[0].image_src"></image>
          </navigator>
          <!-- 右面的四个小块 -->
          <view class="floor_right_img">
            <navigator class="floor_small_img" v-for="(floor,index) in item.product_list.slice(1)" :key="index" :url="floor.url">
              <image class="floor_small_img_img" :src="floor.image_src" mode=""></image>
            </navigator>
          </view>
        </view>
      </view>

    </view>

  </view>
</template>

<script>
  export default {
    data() {
      return {
        swiperList: [], //轮播图数据
        navList: [], //分类导航数据
        floorList: [] //楼梯层数据
      };
    },
    methods: {
      // 发送请求获取轮播图的数据
      async obtainSwiper() {
        const result = await uni.$http.get('/api/public/v1/home/swiperdata')
        if (result.data.meta.status !== 200) return uni.$showMsg()
        this.swiperList = result.data.message
      },
      // 发送请求获取分类导航的数据
      async getNavList() {
        const result = await uni.$http.get('/api/public/v1/home/catitems')
        if (result.data.meta.status !== 200) return uni.$showMsg()
        this.navList = result.data.message
      },
      // 获取楼梯层数据
      async getFloorList() {
        const result = await uni.$http.get('/api/public/v1/home/floordata')
        if (result.data.meta.status !== 200) return uni.$showMsg()
        result.data.message.forEach((value) => {
          value.product_list.forEach(prod => {
            prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
          })
        })
        this.floorList = result.data.message
      },
      // 点击跳转到分类页面
      toCate(item) {
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },

    },
    onLoad() {
      // 渲染轮播图
      this.obtainSwiper()
      // 渲染分类导航
      this.getNavList()
      // 渲染楼梯层
      this.getFloorList()
    }
  }
</script>

<style lang="scss">
  .floor_list {
    .floor_item {
      .floor_item_image {
        margin: 10rpx 0;
      }

      .floor {
        display: flex;

        .floor_left_img {
          width: 50%;
          height: 400rpx;

          .left_HegihtImage {
            width: 100%;
            height: 400rpx;
          }
        }

        .floor_right_img {
          display: flex;
          flex-wrap: wrap;
          width: 100%;
          height: 400rpx;

          .floor_small_img {
            width: 47%;
            height: 50%;
            margin-left: 10rpx;

            .floor_small_img_img {
              width: 100%;
              height: 100%;
            }
          }
        }
      }
    }


  }



  swiper {
    height: 160px;

    .swiper-item,
    image {
      width: 100%;
    }
  }

  .main_nav_list {
    display: flex;
    justify-content: space-around;
    margin: 20rpx 0;

    .nav_image {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floor_list {
    .floor_item_image {
      height: 60rpx;
      width: 100%;
    }
  }
</style>